package com.tigerhhzz.smail.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tigerhhzz.smail.config.AliyunSMSConfig;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author tigerhhzz
 * @Date 2024 10 03 19 45
 **/
@Service
public class SmsService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AliyunSMSConfig aliyunSMSConfig;

    public void sendSms1(String mobile) {
        /**
         * 1.生成6位短信验证码
         * 阿帕奇 提供的工具类
         */
        String code = RandomStringUtils.randomNumeric(6);
        System.out.println(mobile + "收到验证码是：" + code);

        /**
         * 2.将验证码放入redis
         * 此处将code 放入redis仅仅是为了用户输入验证码后，系统后台取出来检验
         */
        redisTemplate.opsForValue().set("smscode_" + mobile, code+"" ,5, TimeUnit.MINUTES );//五分钟过期
        //3.将验证码和手机号发动到rabbitMQ中
        Map<String,String> map=new HashMap<>();
        map.put("mobile", mobile);
        map.put("code", code);
        rabbitTemplate.convertAndSend("sms","sms",map);
    }



    /**
     * 发送短信验证码
     *
     * @param mobile
     * @return
     */
    public String sendSms(String mobile) {

        DefaultProfile profile = DefaultProfile.getProfile(aliyunSMSConfig.getRegionId(),
                this.aliyunSMSConfig.getAccessKeyId(), this.aliyunSMSConfig.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        String code = RandomUtils.nextInt(100000, 999999) + "";

        String redisKey = "CHECK_CODE_" + mobile;
        //短信发送成功，将验证码保存到redis中，有效期为5分钟
        redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(5));

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(aliyunSMSConfig.getDomain());
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", aliyunSMSConfig.getRegionId());
        request.putQueryParameter("PhoneNumbers", mobile); //目标手机号
        request.putQueryParameter("SignName", aliyunSMSConfig.getSignName()); //签名名称
        request.putQueryParameter("TemplateCode", aliyunSMSConfig.getTemplateCode()); //短信模板code
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");//模板中变量替换
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            if (StringUtils.contains(data, "\"Message\":\"OK\"")) {
                return code;
            }
            System.out.println("发送短信验证码失败~ data = " + data);
        } catch (Exception e) {
            System.out.println("发送短信验证码失败~ mobile = " + mobile + e);
        }

        return null;
    }
}
